package org.niux.aims.action;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.niux.aims.common.util.StringUtil;
import org.niux.aims.common.web.AimsBaseAction;
import org.niux.aims.service.IAccountService;
import org.niux.aims.service.ILogsService;
import org.niux.aims.vo.AccountVO;

/**
 * 
 * Copyright (c) 2009,niux<br>
 * All rights reserved.<br>
 * 
 * 文件名称：LoginAction.java<br>
 * 摘    要：登录页面ACTION类<br>
 * <br>
 * 当前版本：<br>
 * 作    者：铭显 Email: <a href="mailto:liangmingxian@gmail.com">liangmingxian@gmail.com</a><br>
 * 完成日期：@since 2013-7-28<br>
 * <br>
 * 取代版本：<br>
 * 原作者  ：<br>
 * 完成日期：@since Oct 22, 2009<br>
 */
public class LoginAction extends AimsBaseAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = 4824932255485850308L;
	private Log log = LogFactory.getLog(this.getClass());

	@Resource(name="accountService")
	private IAccountService accountService;
	@Resource(name="logsService")
	private ILogsService logsService;
	
	private String accountName;
	private String password;
	private String verifyCode;
	private String appid;
	private String msg;
	
	/**
	 * 
	 * 方法概述：登录页面渲染
	 * 描述：登录页面渲染
	 * @return
	 * 返回类型：String
	 * 修改人：梁铭显
	 * 修改时间：2013-7-29
	 */
	public String showPage(){
		if(super.getSession().get("account") != null){
			return "alreadyLogin";
		}
		
		if(msg != null){
			super.getRequest().setAttribute("errMsg", "用户登录超时");
		}
		
		return "success";
	}
	
	/**
	 * 
	 * 方法概述：用户登录
	 * 描述：用户登录
	 * @return
	 * 返回类型：String
	 * 修改人：梁铭显
	 * 修改时间：2013-7-29
	 */
	public String login(){
		log.debug("用户登录");
		
		//判断验证码
		if(!"".equals(verifyCode)){
	        if(!super.getSession().get("RANDOM_CODE_VERIFY").toString().equals(verifyCode)){
	        	super.getRequest().setAttribute("errMsg", "验证码错误");
				return "fails";
	        }
		}
		
		//用户验证
		List<AccountVO> accounts = accountService.findByNameAndApp(accountName, Integer.parseInt(appid));
		
		AccountVO loginAccount = new AccountVO();
		
		if(accounts.isEmpty()){ //账户不存在
			super.getRequest().setAttribute("errMsg", "账户不存在");
			return "fails";
		} else if(accounts.size() > 1){ //账户数据出错
			super.getRequest().setAttribute("errMsg", "账户数据出错");
			return "fails";
		} else {
			for(AccountVO account : accounts){
				if(!account.getPassword().equals(StringUtil.MD5Encode(password))){ //密码错误
					super.getRequest().setAttribute("errMsg", "密码错误");
					return "fails";
				} else {
					loginAccount = account;
				}
			}
		}
		
		super.getSession().put("account", loginAccount);
		logsService.addLog("【"+loginAccount.getName()+"】登录系统");
		
		return "success";
	}
	
	/**
	 * 
	 * 方法概述：退出登录
	 * 描述：退出登录
	 * @return
	 * 返回类型：String
	 * 修改人：梁铭显
	 * 修改时间：2013-7-31
	 */
	public String logout(){
		logsService.addLog("【"+((AccountVO)super.getSession().get("account")).getName()+"】登出系统");
		super.getSession().remove("account");
		return "success";
	}
	
	public String getAccountName() {
		return accountName;
	}

	public void setAccountName(String accountName) {
		this.accountName = accountName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getVerifyCode() {
		return verifyCode;
	}

	public void setVerifyCode(String verifyCode) {
		this.verifyCode = verifyCode;
	}

	public String getAppid() {
		return appid;
	}

	public void setAppid(String appid) {
		this.appid = appid;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}
}
